package com.powerteam.controller.crm;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.crm.ContactsRole;
import com.powerteam.model.crm.Opportunity;
import com.powerteam.service.crm.OpportunityService;
import com.powerteam.service.masterData.OrgUnitService;
import com.powerteam.vo.Result;
import com.powerteam.vo.crm.QueryOpportunityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 机会页面相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/opportunity")
public class OpportunityController extends AuthorizedController {


    @Autowired
    private OpportunityService opportunityService;

    @Autowired
    private OrgUnitService orgUnitService;

    /**
     * 业务机会页面
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String opportunity() {

        return "crm/opportunity";
    }

    /**
     * 创建业务机会
     *
     * @param opportunity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Opportunity opportunity) {

        // 用户Id
        opportunity.setCreateBy(super.getUser().getUserId());
        return opportunityService.insert(opportunity);
    }

    /**
     * 分页查询机会信息
     *
     * @param vo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public PageInfo<Opportunity> find(@RequestBody QueryOpportunityVO vo) {

        List<Integer> subordinate = orgUnitService.findSubordinate(super.getUser().getUserId());
        vo.setUserIdList(subordinate);
        return opportunityService.find(vo);
    }

    /**
     * 业务机会详情
     *
     * @param opportunityId
     * @return
     */
    @RequestMapping(value = "/detail/{opportunityId}", method = RequestMethod.GET)
    public ModelAndView detail(@PathVariable int opportunityId) {

        ModelAndView vm = new ModelAndView("crm/opportunityDetail");
        vm.addObject("opportunityId", opportunityId);
        return vm;
    }

    /**
     * 根据Id查询业务机会详情
     *
     * @param opportunity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Opportunity findById(@RequestBody Opportunity opportunity) {

        return opportunityService.findById(opportunity.getOpportunityId());
    }

    /**
     * 更新/修改业务机会
     *
     * @param opportunity
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/7
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Opportunity opportunity) {
        return opportunityService.update(opportunity);
    }

    /**
     * 添加联系人信息
     *
     * @param contactsRole
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/7
     */
    @ResponseBody
    @RequestMapping(value = "/addContactsRole", method = RequestMethod.POST)
    public Result addContactsRole(@RequestBody ContactsRole contactsRole) {

        return opportunityService.insertContactsRole(contactsRole);
    }

    /**
     * 修改联系人角色
     *
     * @param contactsRole
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/7
     */
    @ResponseBody
    @RequestMapping(value = "/updateContactsRole", method = RequestMethod.POST)
    public Result updateContactsRole(@RequestBody ContactsRole contactsRole) {

        return opportunityService.updateContactsRole(contactsRole);

    }

    /**
     * 移除联系人角色
     *
     * @param contactsRole
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/7
     */
    @ResponseBody
    @RequestMapping(value = "/removeContactsRole", method = RequestMethod.POST)
    public Result removeContactsRole(@RequestBody ContactsRole contactsRole) {

        return opportunityService.deleteContactsRole(contactsRole);
    }

    /**
     * 删除业务机会
     *
     * @param opportunity
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/7
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody Opportunity opportunity) {

        return opportunityService.delete(opportunity);

    }

    /**
     * 历史业务机会
     *
     * @param
     * @return String
     * @Author jinming.zhang
     * @Date 2022/1/8
     */
    @RequestMapping(value = "/history", method = RequestMethod.GET)
    public String history() {

        return "crm/opportunityHistory";
    }

    /**
     * 历史业务机会详情页面
     *
     * @param opportunityId
     * @return ModelAndView
     * @Author jinming.zhang
     * @Date 2022/1/8
     */
    @RequestMapping(value = "/history/view/{opportunityId}", method = RequestMethod.GET)
    public ModelAndView view(@PathVariable int opportunityId) {

        ModelAndView vm = new ModelAndView("crm/opportunityView");
        vm.addObject("opportunityId", opportunityId);
        return vm;
    }

    /**
     * 查看本月我参与的业务机会
     *
     * @param vo
     * @return List<Map>
     * @Author jinming.zhang
     * @Date 2022/1/8
     */
    @ResponseBody
    @RequestMapping(value = "/findMonthlyFunnel", method = RequestMethod.POST)
    public List<Map<String, Integer>> findMonthlyFunnel(@RequestBody QueryOpportunityVO vo) {

        List<Integer> userIdList = new ArrayList<>();
        userIdList.add(super.getUser().getUserId());
        vo.setUserIdList(userIdList);
        return opportunityService.findMonthlyFunnel(vo);
    }

    /**
     * 本月我的转化情况
     *
     * @param vo
     * @return List<Map < String, Integer>>
     * @Author jinming.zhang
     * @Date 2022/1/8
     */
    @ResponseBody
    @RequestMapping(value = "/findMonthlyConversion", method = RequestMethod.POST)
    public List<Map<String, Integer>> findMonthlyConversion(@RequestBody QueryOpportunityVO vo) {

        List<Integer> userIdList = new ArrayList<>();
        userIdList.add(super.getUser().getUserId());
        vo.setUserIdList(userIdList);
        return opportunityService.findMonthlyConversion(vo);
    }

    /**
     * 将要结束的业务机会
     *
     * @param vo
     * @return List<Opportunity>
     * @Author jinming.zhang
     * @Date 2022/1/8
     */
    @ResponseBody
    @RequestMapping(value = "/findRecentlyClosing", method = RequestMethod.POST)
    public List<Opportunity> findRecentlyClosing(@RequestBody QueryOpportunityVO vo) {

        List<Integer> userIdList = new ArrayList<>();
        userIdList.add(super.getUser().getUserId());
        vo.setUserIdList(userIdList);
        return opportunityService.findRecentlyClosing(vo);
    }

}
